﻿package com.taobao.top
{
	import com.adobe.serialization.json.JSON;
	import com.taobao.top.converter.UserConverter;
	import com.taobao.top.events.TOPEvent;
	import com.taobao.top.model.User;
	import com.taobao.top.resultmodel.TradesGetResult;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.*;
	public class TOPUser extends EventDispatcher
	{

		public function TOPUser()
		{
			
		}
		
		public function getUsers(nickNameArr:Array,fieldsParam:Array=null):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.users.get"),new TopRequestParam("nicks",TOPUtil.StringArrayToStringWidthComma(nickNameArr)),new TopRequestParam("fields",TOPUtil.StringArrayToStringWidthComma(fieldsParam))];
			topURLLdr.addEventListener(Event.COMPLETE,usersLoadHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		private function usersLoadHandler(event:Event):void
		{
			switch(event.type)
			{
				case Event.COMPLETE:
					var userArr:Array = usersGetJsonParser(event.target.data);
				break;
				default:
					 dispatchEvent(event.clone());
				break;
			}
		}
		
		public function getUser(nickName:String,fieldsParam:Array = null):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.user.get"),new TopRequestParam("nick",nickName),new TopRequestParam("fields",TOPUtil.StringArrayToStringWidthComma(fieldsParam))];
			topURLLdr.addEventListener(Event.COMPLETE,userLoadHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		private function userLoadHandler(event:Event):void
		{
			
			switch(event.type)
			{
				case Event.COMPLETE:
					var user:User=userGetJsonParser(event.target.data);
					dispatchEvent(new TOPEvent(TOPEvent.TOP_DATA,user));
				break;
				default:
					 dispatchEvent(event);
				break;
			}
		}
		
		
		private function usersGetJsonParser(jsonStr:String):Array
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				return objToUserArray((JSON.decode(jsonStr) as Object).rsp);
			}
			return null;
		}
	
		private function userGetJsonParser(jsonStr:String):User
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				return UserConverter.convert((JSON.decode(jsonStr) as Object).rsp.users[0]);
			}
			
			return null;
		}
		
		private function objToUserArray(rspObj:Object):Array
		{
			var tradesGetResult:TradesGetResult = new TradesGetResult();
			tradesGetResult.totalResults = rspObj.totalResults;
			for each(var obj:Object in rspObj.users)
			{
				userArr.push(UserConverter.convert(obj));
			}
			return userArr;
		}
		
	}
}